package photogcalc.util;

import java.io.IOException;
import java.util.Hashtable;
import javax.microedition.lcdui.Image;
import photogcalc.common.Constants;

/** Works as on-demand image loader and image cache for MIDlet images.
 *
 * @author okkonena
 */
public class ImageProxy {
    private Hashtable images = null;

    /** Default constructor. */
    public ImageProxy() {
        images = new Hashtable(3);
    }

    /** Loads an image from resource folder and returns loaded image.
     *  If the image is already loaded, then it only returns instance
     *  to that. Method also hides resource path and postfix.
     *
     *  @param name images files basename
     *  @return an image
     */
    public Image createImage(String name) throws IOException {
        if (images.contains(name) == false) {
            images.put(
                (String)name,
                (Image)Image.createImage(
                    Constants.RESOURCE_PATH +
                    name +
                    Constants.LIST_IMAGE_POSTFIX
                )
            );
        }

        return (Image)images.get(name);
    }
}
